package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import com.oocl.dao.CustomerDao;
import com.oocl.pojo.Customer;
import com.oocl.pojo.Dish;
import com.oocl.util.DBUtil;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	public int addCustomer(Customer c) {
		String sql="insert into customer(cid,cname,cpwd)values(?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		UUID uuid = null;
		String cId = null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			uuid = UUID.randomUUID();
			cId = uuid.toString();
			pst.setString(1, cId);
			pst.setString(2, c.getcName());
			pst.setString(3, c.getcPwd());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public Customer findCustomerByName(String cName) {
		String sql="select * from customer where cname =?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Customer c =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, cName);
			rs=pst.executeQuery();
			while(rs.next()){
				c=new Customer();
				c.setcId(rs.getString("cid"));
				c.setcName(rs.getString("cname"));
				c.setcPwd(rs.getString("cpwd"));
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return c;
	}

}
